package top.frsf.input;

import top.frsf.GamePanel;
import top.frsf.card.CardObject;
import top.frsf.card.CardStart;
import top.frsf.envi.EnvStart;
import top.frsf.envi.Grand;
import top.frsf.envi.Sun;
import top.frsf.plants.Pease;
import top.frsf.plants.PlaStart;
import top.frsf.utils.GameState;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.Instant;

/**
 * @BelongsProject: MiniPZ
 * @BelongsPackage: top.frsf.inputEvent
 * @Author: FRSF
 * @CreateTime: 2022-09-06  17:29
 * @Description: 鼠标事件
 * @Version: 1.0
 */
public class MouseCont extends MouseAdapter {
    Rectangle click = new Rectangle();
    EnvStart envStart = GamePanel.envStart;
    CardStart cardStart = GamePanel.cardStart;
    PlaStart plaStart = GamePanel.plaStart;
    boolean moveImg = false;

    @Override
    public void mouseMoved(MouseEvent e) {
        // HOME 冒险模式高亮显示
        if (GamePanel.stage == GameState.HOME)
            envStart.home.showHight = envStart.home.game.contains(e.getPoint());

        if (moveImg) {
            cardStart.peaseCard.movePoint = e.getPoint();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println(e.getPoint());
        click.setBounds(e.getX(), e.getY(), 20, 20);

        // HOME开启游戏,（后续程序不执行）
        if (GamePanel.stage == GameState.HOME || envStart.home.game.contains(e.getPoint())) {
            GamePanel.stage = GameState.START;
        }


        // 点击拖拽图片后判断放置, 此时正在移动植物，(后续程序不执行)
        if (moveImg) {
            // 遍历草地，找到能安放的位置
            for (Grand grand : EnvStart.grandList) {
                if (grand.plant && click.intersects(grand.getRec())) {
                    // 草地可种植 且 为此时点击的草地
                    grand.plant = false;
                    plaStart.plantList.add(new Pease("", grand.getX() + 6, grand.getY() + 10, 70, 70));
                    moveImg = false;
                    // 取消植物拖拽跟随
                    cardStart.peaseCard.movePoint = null;
                    return;
                }
            }
        }


        // 阳光收集，(后续不执行)
        for (Sun sun : GamePanel.envStart.sunList) {
            if (click.intersects(sun.getRec())) {
                GamePanel.envStart.removeSunList.add(sun);
                GamePanel.envStart.sunSum += 50;
                return;
            }
        }

        // 植物卡片点击遍历
        for (CardObject card : cardStart.cardList) {
            if (click.intersects(card.getRec())) {
                // 点击其中一个卡片
                if (envStart.sunSum >= card.price && !card.isCD) {
                    // 资金足够 且 卡片未处于cd中
                    envStart.sunSum -= card.price;
                    // 开启冷却
                    card.isCD = true;
                    // 记录点击时的时间戳
                    card.setClick(Instant.now());
                    // 开启植物点击拖拽图像
                    card.movePoint = new Point(e.getPoint());
                    // 开启mouseMove监视
                    moveImg = true;
                    break;
                } else return;
            }
        }
    }
}
